<?php

namespace backend\models\user;

use Yii;
use backend\models\user\User;
/**
 * This is the model class for table "{{%user}}".
 *
 * @property integer $user_id
 * @property integer $user_num
 * @property string $user_email
 * @property string $user_phone
 * @property string $user_name
 * @property string $user_pwd
 * @property integer $user_status
 * @property integer $user_regtime
 * @property integer $user_lastlogin
 * @property string $user_regip
 * @property string $user_lastip
 * @property integer $user_updtime
 * @property integer $user_sex
 * @property string $user_sign
 * @property integer $user_virtual
 * @property integer $province_id
 * @property integer $city_id
 * @property integer $area_id
 * @property string $user_thumb
 * @property integer $user_qq
 * @property string $user_emid
 * @property string $user_openid
 * @property string $user_opentype
 */
class Users extends User
{
	public $user_repwd;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_name','user_email','user_pwd','user_repwd',], 'required','message'=>'{attribute}不能为空'],
            [['user_email'], 'string', 'max' => 40],
            [['user_name'], 'string', 'max' => 18],
            [['user_pwd'], 'string', 'max' => 32],
			 [['user_pwd'], 'string','min'=>5, 'max' => 20,'message'=>'{attribute}位数为5至20位'],
            ['user_email', 'unique', 'message' => '邮箱名已存在.'],
			 ['user_email','match','pattern'=>'/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/','message'=>'{attribute}格式不对'],
			['user_repwd','compare','compareAttribute'=>'user_pwd','message'=>'两次密码不一致'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
           // 'user_id' => 'User ID',
            //'user_num' => 'User Num',
            'user_email' => '邮箱',
           // 'user_phone' => 'User Phone',
            'user_name' => '用户名',
            'user_pwd' => '密码',
            /*'user_status' => 'User Status',
            'user_regtime' => 'User Regtime',
            'user_lastlogin' => 'User Lastlogin',
            'user_regip' => 'User Regip',
            'user_lastip' => 'User Lastip',
            'user_updtime' => 'User Updtime',
            'user_sex' => 'User Sex',
            'user_sign' => 'User Sign',
            'user_virtual' => 'User Virtual',
            'province_id' => 'Province ID',
            'city_id' => 'City ID',
            'area_id' => 'Area ID',
            'user_thumb' => 'User Thumb',
            'user_qq' => 'User Qq',
            'user_emid' => 'User Emid',
            'user_openid' => 'User Openid',
            'user_opentype' => 'User Opentype',*/
			'user_repwd' => '确认密码'
        ];
    }
	/**
	* 查询
	*/
	public function UserCount($code)
	{
		return $this->find()
					->where($code)
					->count();
	}
	/**
	* 搜索查询
	*/
	public function UserSearch($code,$pagination)
    {
		return $this->find()
				->where($code)
				->orderBy(['user_regtime'=>SORT_DESC])
				->offset($pagination->offset)
				->limit($pagination->limit)
				->asArray()
				->all();
	}
	/**
	* 查询单条
	*/
	public function User($user_id)
	{
		return $this->find()
				->where(['=','user_id',$user_id])
				->asArray()
				->one();
	}
	/**
	* 修改禁用状态
	*/
	public function UserStatus($user_id,$status)
	{
		return $this->updateAll(['user_status' => $status],'user_id=:user',[':user'=>$user_id]);
	}
	/**
	* ip地址接口
	*/
	public function getIPLoc_sina($queryIP){ 
		$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryIP; 
		$ch = curl_init($url); 
		curl_setopt($ch,CURLOPT_ENCODING ,'utf8'); 
		curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 
		$location = curl_exec($ch); 
		$location = json_decode($location); 
		curl_close($ch);
		$loc = ""; 
		if($location===FALSE) return ""; 
		if (empty($location->desc)) { 
		$loc = $location->province.$location->city.$location->district.$location->isp;
		}else{
		$loc = $location->desc; 
		}
		return $loc;
	}
}
